package com.tdam2013.grupo07.networking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.tdam2013.grupo07.util.Constantes;

public class ServerHTTPInterface 
{
	public static final int STATUS_ERROR_USERNAME_OR_PASSWORD_TOO_SHORT=13;
	
	public String registerUser(String username, String password)
	{
		String requestString=String.format("<action id=\"%s\" name=\"register-user\"><action-detail><user username=\"%s\" password=\"%s\"/></action-detail></action>", Calendar.getInstance().getTimeInMillis(), username, password);
	    return postRequest(requestString);
	}
	
	public String sendMessage(String sender, String password,String recipient, String message)
	{
		String requestString=String.format("<action id=\"%s\" name=\"send-message\"><action-detail><auth username=\"%s\" key=\"%s\"></auth><message to=\"%s\"><![CDATA[%s]]></message></action-detail></action>", Calendar.getInstance().getTimeInMillis(), sender, password, recipient, message);
	    return postRequest(requestString);
	}
	
	public String queryMessages(String user, String password, String timestamp)
	{
		String requestString=String.format("<action id=\"%s\" name=\"get-messages\"><action-detail><auth username=\"%s\" key=\"%s\"></auth><filter type=\"timestamp\">%s</filter></action-detail></action>", Calendar.getInstance().getTimeInMillis(), user, password, timestamp);
	    return postRequest(requestString);
	}
	
	private String postRequest(String requestString)
	{
		StringBuilder responseStr = null;
	    HttpClient client = null;
	    HttpPost request = null;
	    HttpResponse response = null;
	    InputStream in = null;
	    responseStr = new StringBuilder();
	    try
	    {
	      client = new DefaultHttpClient();
	      String url=Constantes.SERVER_URL;
	      request = new HttpPost(url);
	      StringEntity body= new StringEntity(requestString, "ISO-8859-1");
	      request.setEntity(body);
	      response = client.execute(request);
	      in = response.getEntity().getContent();
	      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
	      String line = null;
	      while ((line = reader.readLine()) != null) 
	      {
	        responseStr.append(line + "\n");
	      }
	    } catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    finally 
	    {
	      if (in != null) 
	      {
	        try 
	        {
	          in.close();
	        } 
	        catch (Exception e) 
	        {
	        }
	      }
	    }
	    return responseStr.toString();
	}
}
